<?php
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.

// Further corrections and modifications to use a button from David Vielmetter
//  http://davidvielmetter.com/tricks/howto-convert-an-html-table-to-csv-using-php/

// Check if we got something
$export_rows = $_REQUEST['export_rows'];

if (empty($export_rows))
	die('No data to save!');
	
$data = unserialize($export_rows);

if (empty($data))
	die('Unserializing data failed!');

// filename for download
$filename = "engine-run-" . date('Y-m-d-H-i-s') . ".csv";

header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv; charset=UTF-8");
// header("Content-type: application/vnd.ms-excel");
// header("Content-type: application/octet-stream");
header("Pragma: no-cache");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Expires: 0");

$out = fopen("php://output", 'w');

fputcsv($out, array("PointX", "PointY", "PointZ"));
foreach ($data as $data_row)
{
	fputcsv($out, $data_row); // each line should be an array
}

fclose($out);
?>